package red.stu.mc.mod.codeblock.tileentity;

import javax.script.ScriptEngine;

import net.minecraft.block.Block;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import red.stu.mc.mod.codeblock.block.BlockCodeBlock;
import red.stu.mc.mod.codeblock.util.CodeBlockScriptEngineManager;

public class TileEntityCodeBlock extends TileEntity {
	
	private boolean powered;
	private long lastRunTime = -1; // 代码最后一次执行时间
	private String code = ""; // 执行代码
	private String group = "NONE"; // 所属群组
	private String var = ""; // 序列化变量

	/*// 脚本引擎工厂
	private static final CodeBlockScriptEngineManager SCRIPT_ENGINE_MANAGER = new CodeBlockScriptEngineManager();
	// 脚本引擎
	private ScriptEngine scriptEngine;*/

	public void setPowered(boolean poweredIn) {
		this.powered = poweredIn;
	}

	public boolean isPowered() {
		return this.powered;
	}

	public long getLastRunTime() {
		return lastRunTime;
	}

	public void setLastRunTime(long lastRunTime) {
		this.lastRunTime = lastRunTime;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	@Override
	public void validate() {
		// scriptEngine = SCRIPT_ENGINE_MANAGER.getScriptEngine(group);
		super.validate();
	}

	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
		if (CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.equals(capability)) {
			return true;
		}
		return super.hasCapability(capability, facing);
	}

	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
		if (CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.equals(capability)) {
			// return your IItemHandler
		}
		return super.getCapability(capability, facing);
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setBoolean("powered", this.powered);
		compound.setLong("lastRunTime", lastRunTime);
		compound.setString("code", this.code);
		compound.setString("group", this.group);
		if (this.var == null) {
			if (compound.hasKey("var"))
				compound.removeTag("var");
		} else {
			compound.setString("var", this.var);
		}
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.powered = compound.getBoolean("powered");
		this.lastRunTime = compound.getLong("lastRunTime");
		this.code = compound.getString("code");
		this.group = compound.getString("group");
		this.var = compound.getString("var");
	}

	@Override
	public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
		return !oldState.getBlock().equals(newState.getBlock());
	}

}
